package com.cdd.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdd.index.config.NewPageHelp;
import com.cdd.index.mapper.BottomArticleMapper;
import com.cdd.index.service.IBottomArticleService;
import com.common.entity.BottomArticle;
import com.common.untils.CommonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class BottomArticleServiceImpl extends ServiceImpl<BottomArticleMapper, BottomArticle> implements IBottomArticleService {

    @Autowired
    private BottomArticleMapper bottomArticleMapper;

    @Override
    public CommonResult selectAll(Integer currentPage, Integer pageSize, BottomArticle bottomArticle) {
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(bottomArticle.getName())){
            wrapper.like("name",bottomArticle.getName());
        }
        if (bottomArticle.getIsDisable()!=null){
            wrapper.eq("is_disable",bottomArticle.getIsDisable());
        }
        //获取所有的bottom
        List<BottomArticle> bottomAll = bottomArticleMapper.selectList(wrapper);

        List<BottomArticle> firstBottom = new ArrayList<>();
        for (BottomArticle frist: bottomAll) {
            if (frist.getParentId().equals("0")){
                firstBottom.add(frist);
            }
        }
        for (BottomArticle first1 :firstBottom) {
            //根据一级菜单id 查询 该菜单得二级菜单。如果出现不确定有几级菜单 那么我们可以使用方法得递归调用
            first1.setChildren(findChildren(bottomAll, first1.getId()));
        }
        PageInfo pageInfo = NewPageHelp.pageHelper(firstBottom,currentPage,pageSize);

        return new CommonResult(2000,"查询成功",pageInfo);

    }

    @Override
    public CommonResult findAll() {
        QueryWrapper<BottomArticle> wrapper =new QueryWrapper<>();
        wrapper.eq("is_disable",0);
        wrapper.eq("is_deleted",0);
        //获取所有的bottom
        List<BottomArticle> bottomAll = bottomArticleMapper.selectList(wrapper);
        List<BottomArticle> firstBottom = new ArrayList<>();
        for (BottomArticle frist: bottomAll) {
            if (frist.getParentId().equals("0")){
                firstBottom.add(frist);
            }
        }
        for (BottomArticle first1 :firstBottom) {
            //根据一级菜单id 查询 该菜单得二级菜单。如果出现不确定有几级菜单 那么我们可以使用方法得递归调用
            first1.setChildren(findChildren(bottomAll, first1.getId()));
        }
        return new CommonResult(2000,"查询成功",firstBottom);
    }

    @Override
    public CommonResult updateStatus(String id, Integer isDisable) {
        BottomArticle bottomArticle = new BottomArticle();
        bottomArticle.setId(id);
        bottomArticle.setIsDisable(isDisable);
        bottomArticleMapper.updateById(bottomArticle);
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<BottomArticle> bottomArticles = bottomArticleMapper.selectList(wrapper);
        if (null!=bottomArticles&&!"".equals(bottomArticles)){
            for (BottomArticle b1 :bottomArticles){
                BottomArticle bottomArticle1 = new BottomArticle();
                bottomArticle1.setId(b1.getId());
                bottomArticle1.setIsDisable(isDisable);
                bottomArticleMapper.updateById(bottomArticle1);
            }
        }

        return new CommonResult(2000,"修改状态成功",null);
    }

    @Override
    public CommonResult updateBottomArticle(BottomArticle bottomArticle) {
        bottomArticleMapper.updateById(bottomArticle);
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult deleteBottomArticle(String id) {

        bottomArticleMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",null);
    }


    @Override
    public CommonResult BottomArticleAdd(BottomArticle bottomArticle) {
        bottomArticle.setIsDeleted(0);
        bottomArticle.setIsDisable(0);
        bottomArticleMapper.insert(bottomArticle);
        return new CommonResult(2000,"添加成功",null);
    }






    //递归

    private List<BottomArticle> findChildren (List < BottomArticle > bottomList, String id){
        List<BottomArticle> children = new ArrayList<>();
        for (BottomArticle p : bottomList) {
            if (p.getParentId().equals(id)) {
                children.add(p);
            }
        }
        for (BottomArticle child : children) {
            child.setChildren(findChildren(bottomList, child.getId()));
        }
        //System.out.println("-----------"+children);
        return children;

    }
}
